using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLibrary;
using SynapseGaming.SunBurn.Framework.Content;
using SynapseGaming.SunBurn.Framework.Core;
using SynapseGaming.SunBurn.Framework.Graphics;
using SynapseGaming.SunBurn.Framework.Models;

using TargetPlatform = SynapseGaming.SunBurn.Framework.Driver.DX11.DX11Platform;
using System.Windows.Forms;
using System.IO;
using SynapseGaming.SunBurn.Framework.Primitives;


namespace WindowsDesktop
{
	/// <summary>
    /// Entry point for games utilizing the built-in game loop
    /// (via Platform.RunAsGameLoop.Run()).
    /// 
    /// The platform layer automatically initializes itself and
    /// any needed platform resources (like a window, graphics device, ...).
    /// </summary>
    public class WindowsDesktopProgram : BaseGameProgram
    {
		/// <summary>
        /// Main entry point for the game / application.
        /// </summary>
        [STAThreadAttribute]
        private static void Main()
        {
            if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.DoEvents();

            MainForm form = new MainForm();
            Application.Run(form);
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern bool SetProcessDPIAware();

        public override System.IO.Stream OpenFileStream(string fileName, bool open)
        {
            return new FileStream(fileName, open ? FileMode.Open : FileMode.Create);
        }

        public override SynapseGaming.SunBurn.Framework.Graphics.Textures.Texture2D LoadExternalTexture(string fileName)
        {
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(fileName);

            SynapseGaming.SunBurn.Framework.Graphics.Textures.Texture2D texture =
                Platform.Instance.GraphicsDevice.CreateTexture2D(bitmap.Width,
                bitmap.Height, true, false, SynapseGaming.SunBurn.Framework.Graphics.Textures.SurfaceFormat.Color, false);

            Color[] colors = new Color[bitmap.Width * bitmap.Height];

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    System.Drawing.Color bitColor = bitmap.GetPixel(x, y);

                    colors[x + y * bitmap.Width] = new Color(bitColor.R, 
                        bitColor.G, bitColor.B, bitColor.A);
                }
            }

            texture.SetData(colors);

            return texture;
        }
    }
}
